﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Phones.Client.Model;
using System.Collections.ObjectModel;

namespace Phones.Client.Repository
{
    public class ContactsRepository : BaseRepository<Contact>, IDisposable
    {
        public ContactsRepository(PhonesEntities context) : base(context) { }
        public ContactsRepository() : base() { }

        public List<Contact> GetList(ServiceNode node, Department department)
        {
            if (node == null) return null;
            else
            {
                var query = from c in Context.Contacts
                            .Include("Telephones")
                            .Include("People")
                            .Include("Telephones")
                            where c.City.ServiceNode.ID == node.ID && c.Department.ID == department.ID
                            orderby c.Name ascending
                            select c;
                var list = query.Distinct().ToList();
                return list;
            }
        }

        public Contact GetById(int id)
        {
            var item = Context.Contacts.FirstOrDefault(x => x.ID == id);
            Context.Detach(item);
            return item;
        }
    }
}
